package com.shuyun.springdata.showcase.spesc;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

import javax.persistence.EntityManager;
import java.io.Serializable;

/**
 * Created by feng.ding on 2017/1/9.
 */
public class CustomerRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T, S, ID>{
    @Override
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
        return new CustomerRepositoryFactory(entityManager);
    }

   private static class CustomerRepositoryFactory extends JpaRepositoryFactory{

       public CustomerRepositoryFactory(EntityManager entityManager) {
           super(entityManager);
       }

       @Override
       protected <T, ID extends Serializable> SimpleJpaRepository<?, ?> getTargetRepository(RepositoryInformation information, EntityManager entityManager) {
           return new CustomerRepositoryImpl<T, ID>((Class<T>)information.getDomainType(), entityManager);
       }

       @Override
       protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
           return CustomerRepositoryImpl.class;
       }
   }
}
